import time
import json
import requests
import re
import pymysql


def get_time():
    time_str = time.strftime("%Y{}%m{}%d  %X")
    return time_str.format("年", "月", "日")

def get_data():
  url = "https://voice.baidu.com/act/newpneumonia/newpneumonia/?from=osari_aladin_banner"
  headers = {
    'authority': 'voice.baidu.com',
    'cache-control': 'max-age=0',
    'sec-ch-ua': '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
    'sec-ch-ua-mobile': '?0',
    'upgrade-insecure-requests': '1',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'sec-fetch-site': 'same-origin',
    'sec-fetch-mode': 'navigate',
    'sec-fetch-user': '?1',
    'sec-fetch-dest': 'document',
    'referer': 'https://voice.baidu.com/act/newpneumonia/newpneumonia/?from=osari_aladin_banner',
    'accept-language': 'zh-CN,zh;q=0.9',
    'cookie': 'BIDUPSID=AC2B25A8B486C63D84BFB30EE05B7E7D; PSTM=1600848360; BAIDUID=D299D5728A2CF5E6F8344E716DC7FEB1:FG=1; __yjs_duid=1_d4f38ffef0db537fa940e7f06c28cf661619589350550; H_WISE_SIDS=107319_110085_127969_128698_131423_154214_165136_166148_170142_170817_170936_171234_171509_171573_172472_172644_172867_173016_173633_174036_174324_174436_174449_174549_174638_174662_174665_174694_174856_174909_175030_175045_175213_175275_175284_175365_175555_175730_175749_175819_175859_175899_175974; BDUSS=R6ekFKWnJoZ2JwS0VvQkgyMmhOVWtGZk04cWx-QjFTU0xsQU85ZHJOVn5iOGhnRVFBQUFBJCQAAAAAAAAAAAEAAAAVxpHLbXpowvMxMgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH~ioGB~4qBgNW; BDUSS_BFESS=R6ekFKWnJoZ2JwS0VvQkgyMmhOVWtGZk04cWx-QjFTU0xsQU85ZHJOVn5iOGhnRVFBQUFBJCQAAAAAAAAAAAEAAAAVxpHLbXpowvMxMgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH~ioGB~4qBgNW; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; delPer=0; PSINO=7; BAIDUID_BFESS=D299D5728A2CF5E6F8344E716DC7FEB1:FG=1; BDRCVFR[feWj1Vr5u3D]=mk3SLVN4HKm; H_PS_PSSID=34132_33763_34223_31254_34004_34113_33607_34134; lscaptain=srcactivitycaptainindexcss_91e010cf-srccommonlibsesljs_e3d2f596-srcactivitycaptainindexjs_a2e9c712; Hm_lvt_68bd357b1731c0f15d8dbfef8c216d15=1625048941; Hm_lpvt_68bd357b1731c0f15d8dbfef8c216d15=1625058941'
  }
  response = requests.request("GET", url, headers=headers).text
  pat = '"summaryDataIn":(.*?),"summaryDataOut"'
  content = re.compile(pat).findall(response)
  case_pat = 'caseList":(.*?),"caseOutsideList"'
  caseList = re.compile(case_pat).findall(response)
  caselist = caseList[0][1:-1]
  print(caselist[0])
  return content[0]


def get_map_data():
    db = pymysql.connect(host="119.23.40.47", user="XXX", passwd="XXX", db="cov19", charset="utf8")
    cursor = db.cursor()
    sql = 'select * from proSit'
    cursor.execute(sql)
    results = cursor.fetchall()
    res = []
    for i in results:
        res.append({"name":i[1], "value": i[2]})
    db.commit()
    db.close()
    return res

def spider_vaccine_data():
    dict = {}
    url = 'https://api.inews.qq.com/newsqa/v1/automation/modules/list?modules=VaccineTopData'
    res = requests.post(url).json()
    data = res['data']['VaccineTopData']
    c_num = str(float(data['中国']['total_vaccinations'])/100000000)[:4]
    c_per = data['中国']['total_vaccinations_per_hundred']
    a_num = str(float(data['全球']['total_vaccinations'])/100000000)[:4]
    a_per = data['全球']['total_vaccinations_per_hundred']
    dict['c_num'] = c_num
    dict['c_per'] = c_per
    dict['a_num'] = a_num
    dict['a_per'] = a_per
    return dict


if __name__ == '__main__':
    spider_vaccine_data()